package com.neusoft.elmboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neusoft.elmboot.exception.exceptions.ErrorStateException;
import com.neusoft.elmboot.mapper.PointMapper;
import com.neusoft.elmboot.mapper.PointTransactionMapper;
import com.neusoft.elmboot.po.point.Point;
import com.neusoft.elmboot.po.point.PointTransaction;
import com.neusoft.elmboot.po.vo.PointTransactionVO;
import com.neusoft.elmboot.service.PointTransactionService;
import com.neusoft.elmboot.util.PointUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class PointTransactionServiceImpl extends ServiceImpl<PointTransactionMapper, PointTransaction>
        implements PointTransactionService {

    @Resource
    private PointMapper pointMapper;

    @Resource
    private PointTransactionMapper pointTransactionMapper;

    private void checkUser(String userId){
        Point point = pointMapper.selectById(userId);
        if (point == null) {
            throw new ErrorStateException("用户不存在");
        }
    }

    @Override
    public List<PointTransactionVO> getPointTrans(String userId, Integer index) {
        checkUser(userId);
        if (index.equals(PointUtils.ADD_INDEX)) {
            return pointTransactionMapper.getIncomeTrans(userId);
        } else if (index.equals(PointUtils.CONSUME_INDEX)) {
            return pointTransactionMapper.getExchangeTrans(userId);
        } else {
            return pointTransactionMapper.getAllTrans(userId);
        }
    }
}
